package com.buding.fx.view;

import com.buding.fx.controller.codeformat.CodeFormatJavaController;
import com.buding.fx.controller.codeformat.CodeFormatMySqlController;
import com.buding.fx.service.CodeFormatCreateThread;
import com.buding.fx.utils.CaiPiaoCreateThread;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.svg.SVGGlyph;
import com.jfoenix.svg.SVGGlyphLoader;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.plaf.basic.BasicButtonUI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CodeFormatStart extends Application {
    private Logger logger = LoggerFactory.getLogger(CodeFormatStart.class);
    private double rootWight = 850;  //总体宽
    private double rootheight = 800;  //总
    private Map<String, SVGGlyph> svgMap = new HashMap<>();

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane borderPane = new BorderPane();
        borderPane.setPrefWidth(rootWight);
        borderPane.setPrefHeight(rootheight);
        borderPane.setStyle("-fx-background-color: #ffffff;");
        FlowPane flowPane = new FlowPane();
        flowPane.setPrefWidth(borderPane.getPrefWidth());
        flowPane.setPrefHeight(35);
        flowPane.getStyleClass().add("code_flow");


        Label label = new Label("JAVA");
        label.setPrefWidth(80);
        label.setPrefHeight(35);
        label.getStyleClass().add("code_lab");
        label.setAlignment(Pos.CENTER);
        label.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //异步更新左边组件
                Platform.runLater(new CodeFormatCreateThread(borderPane,label,svgMap, primaryStage));

            }
        });


        Label label1 = new Label("MYSQL");
       // label1.setBorder(border);
        label1.setPrefWidth(80);
        label1.setPrefHeight(35);
        label1.getStyleClass().setAll("code_lab");
        label1.setAlignment(Pos.CENTER);
        label1.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //异步更新左边组件
                Platform.runLater(new CodeFormatCreateThread(borderPane,label1,svgMap,primaryStage));
            }
        });
        //默认单选
        label.fireEvent(new MouseEvent(MouseEvent.MOUSE_CLICKED,1, 1, 1, 1, MouseButton.PRIMARY, 1, true, true, true, true, true,
                true, true, true, true, true, null));

        flowPane.getChildren().addAll(label,label1);
        borderPane.setTop(flowPane);


        Scene scene = new Scene(borderPane,rootWight,rootheight);
        primaryStage.setTitle("代码生成工具");
        primaryStage.setScene(scene);
        primaryStage.show();
        String css1 = "css/code-format-main.css";
        String css2 = "css/jfoenix-main-demo.css";
        scene.getStylesheets().add(css1);
        scene.getStylesheets().add(css2);
        scene.getStylesheets().add("org/kordamp/bootstrapfx/bootstrapfx.css"); //(3)

    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {

        Screen screen = Screen.getPrimary();

        // 获取当前屏幕的宽度和高度
        double width = screen.getVisualBounds().getWidth();
        double height = screen.getVisualBounds().getHeight();
        logger.info("当前电脑的长：{},宽：{}: ",height,width);

        this.rootheight = height * 0.85;
        this.rootWight = width * 0.7;
        logger.info("main  init: {}",3);

        SVGGlyphLoader.loadGlyphsFont(CodeFormatView.class.getResourceAsStream("/fonts/icomoon.svg"),
                "icomoon.svg");
        Set<String> allGlyphsIDs = SVGGlyphLoader.getAllGlyphsIDs();
        for (String allGlyphsID : allGlyphsIDs) {
            SVGGlyph glyph = SVGGlyphLoader.getGlyph(allGlyphsID);
            svgMap.put(glyph.getGlyphId()+"",glyph);
        }
    }
}
